//
//  HHMyMoneyViewController.m
//  HHBroker
//
//  Created by He Wei on 10/18/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHMyMoneyViewController.h"
#import "HHMyMoneyCell.h"
#import "HHMyMoneyModel.h"
#import "HHAddBankCardHeaderView.h"
#import "BankCardInfoCell.h"
#import "HHMonthMoneyViewController.h"
#import "HHAddBankCardHeaderView.h"

#define KAlertTagChangePaYMent 1001

@interface HHMyMoneyViewController () <HHAddBankCardHeaderViewDelegate, UINavigationControllerDelegate>
// 佣金总数
@property (nonatomic, copy) NSString *amount;
@property (nonatomic, assign) BOOL isDownloadedData;
@property (nonatomic, assign) BOOL isHaveBankCard;

@property (nonatomic, strong) HHAddBankCardHeaderView *headerView;


@end

@implementation HHMyMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareData];
    if ([JiaYuanShareData sharedInstance].bankCardNumber.length == 0) {
        self.tableView.tableHeaderView = self.headerView;
        self.isHaveBankCard = NO;
    } else {
        self.isHaveBankCard = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Delegate

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseAllMoneyCell = @"reuseAllMoneyCell";
    static NSString *reuseListMoneyCell = @"reuseListMoneyCell";
    static NSString *reuseBankCardCell = @"reuseBankCardCell";
    
    if (self.isHaveBankCard) {
        if (indexPath.section == 0) {
            BankCardInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseBankCardCell];
            if (cell == nil) {
                cell = [[BankCardInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseBankCardCell];
            }
            cell.bankCardNumber = [JiaYuanShareData sharedInstance].bankCardNumber;
            return cell;
        }
        if (indexPath.section == 1) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseAllMoneyCell];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseAllMoneyCell];
                
            }
            cell.textLabel.text = @"我的佣金";
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", self.amount];
            return cell;
        }
    }
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseAllMoneyCell];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseAllMoneyCell];
        }
        cell.textLabel.text = @"我的佣金";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", self.amount];
        return cell;
    }
    HHMyMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseListMoneyCell];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HHMyMoneyCell" owner:self options:nil] lastObject];
    }
    HHMyMoneyModel *model = self.dataArray[indexPath.section];
    cell.model = model;
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isHaveBankCard) {
        if (indexPath.section == 0) {
            [self changeBankCardButtonClicked:nil];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            return;
        }
    }
    HHMyMoneyModel *model = self.dataArray[indexPath.section];
    if (![model isKindOfClass:[HHMyMoneyModel class]]) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        return;
    }
    self.index = indexPath;
    HHMonthMoneyViewController *month = [[HHMonthMoneyViewController alloc] init];
    month.month = model.happen_time;
    month.model = model;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isHaveBankCard) {
        if (indexPath.section == 0) {
            return 56;
        }
        if (indexPath.section == 1) {
            return 44.0f;
        }
    } else {
        if (indexPath.section == 0) {
            return 44.0f;
        }
    }
    
    return [HHMyMoneyCell heightWithModel:nil];
    
}

//#pragma mark - UIAlertViewDelegate
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if (alertView.tag == KAlertTagChangePaYMent) {
//        if (buttonIndex == 1) {
//            [self toggleToChangePayMentCard];
//        }
//    }
//}
//
//
//#pragma mark - AddBankCardHeaderViewDelegate
//- (void)headerViewDidTouched:(AddBankCardHeaderView *)headerView
//{
//    [self changePayMentCardWithTitle:@"确认添加银行卡"];
//    
//    
//}
//
//#pragma mark - JYAddBankCardViewControllerDelegate
//- (void)addBankCardViewControllerDidCancel:(JYAddBankCardViewController *)controller
//{
//    [controller dismissViewControllerAnimated:YES completion:nil];
//}
//- (void)addBankCardViewController:(JYAddBankCardViewController *)controller didFinishAddingCard:(NSString *)cardNumber editingInfo:(NSDictionary *)editingInfo
//{
//    [controller dismissViewControllerAnimated:YES completion:^{
//        if (self.tableView.tableHeaderView) {
//            self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
//            self.isHaveBankCard = YES;
//            // 占位
//            [self.dataArray addObject:[NSNull null]];
//            [self.tableView reloadData];
//            // 上面0.01高的view的作用是为了reload的时候table的位置正常, reload以后把header置nil
//            self.tableView.tableHeaderView = nil;
//            return;
//        }
//        BankCardInfoCell *cell = (BankCardInfoCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//        if ([cell isKindOfClass:[BankCardInfoCell class]]) {
//            cell.bankCardNumber = cardNumber;
//        }
//    }];
//    
//}
#pragma mark - Event

- (void)changeBankCardButtonClicked:(UIButton *)button
{
    [self changePayMentCardWithTitle:@"确认修改银行卡"];
}

- (void)changePayMentCardWithTitle:(NSString *)title
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:[NSString stringWithFormat:@"我们将发送短信验证码到您的账户绑定的手机上"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好", nil];
    alert.tag = KAlertTagChangePaYMent;
    [alert show];
}

//- (void)toggleToChangePayMentCard
//{
//    __block MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    hub.mode = MBProgressHUDModeIndeterminate;
//    // 网络请求
//    // 加入加密字段
//    NSString *urlString = [NSString stringWithFormat:@"%@", KUrlCBindCardGetCode];
//    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
//        
//        NSInteger code = [[response objectForKey:@"code"] integerValue];
//        if (code == 1) {
//            // 发送成功
//            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
//            hub.mode = MBProgressHUDModeCustomView;
//            hub.customView = imageView;
//            hub.labelText = NSLocalizedString(@"发送成功，请查看手机短信", nil);;
//            hub.completionBlock = ^ {
//                JYAddBankCardViewController *addBank = [[JYAddBankCardViewController alloc] init];
//                addBank.delegate = self;
//                [self presentViewController:addBank animated:YES completion:nil];
//            };
//            [hub hide:YES afterDelay:1];
//            
//        } else {
//            hub.mode = MBProgressHUDModeText;
//            hub.labelText = [response objectForKey:@"msg"];
//            [hub hide:YES afterDelay:2];
//            
//        }
//    } failure:^(NSError *error) {
//        hub.mode = MBProgressHUDModeText;
//        hub.labelText = @"网络有点问题!";
//        [hub hide:YES];
//    } isLogin:YES];
//}
//
//
#pragma mark - Setter && Getter

- (void)prepareData
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [HttpRequestManager getWithUrlString:KUrlListMoney success:^(id response) {
        [hub hide:YES];
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            self.amount = [response objectForKey:@"amount"];
            // index = 0 佣金 占位
            [self.dataArray addObject:[NSNull null]];
            if (self.isHaveBankCard) {
                // 银行卡 占位
                [self.dataArray addObject:[NSNull null]];
            }
            id data = [response objectForKey:@"list"];
            if ([data isKindOfClass:[NSArray class]]) {
                NSArray *list = (NSArray *)data;
                for (NSDictionary *dic in list) {
                    HHMyMoneyModel *model = [[HHMyMoneyModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.dataArray addObject:model];
                }
            }
            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"网络有点问题呢!";
        [hub hide:YES afterDelay:2];
    } cache:NO];
}
//
//- (AddBankCardHeaderView *)headerView
//{
//    if (_headerView == nil) {
//        _headerView = [AddBankCardHeaderView headerView];
//        _headerView.delegate = self;
//    }
//    return _headerView;
//}



@end
